package com.zprmb.udzt;

import java.text.DecimalFormat;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class ExperienceGoldProduct extends BaseActivity implements OnClickListener{
	private String id,time,way,rate;
	private TextView rate_of_return,tv_customer;
	private TextView tv_amount;
	DecimalFormat df = new DecimalFormat("###.00");  
	private Button btn_invest;
	private int flag;
	private RelativeLayout rl_detail,rl_back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exp_product);
		id=getIntent().getStringExtra("id");
		flag=getIntent().getIntExtra("flag", 0);
		initview();
		initData();
	}
	private void invest(){
		AjaxParams params=new AjaxParams();
		params.put("userExpId", id);
		
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("invest", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					Intent intent1=new Intent(ExperienceGoldProduct.this,MyExperienceGold.class);
					intent1.putExtra("flag", flag);
					 startActivity(intent1);
					 ExperienceGoldProduct.this.finish();
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.execute(ExperienceGoldProduct.this, Constant.URL+"/api/v5/experience/sureExpInvest?", params.toString());
	}
	private void initview() {
		// TODO Auto-generated method stub
		tv_amount=(TextView)findViewById(R.id.tv_amount);
		rate_of_return=(TextView)findViewById(R.id.rate_of_return);
		btn_invest=(Button)findViewById(R.id.btn_invest);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_detail=(RelativeLayout)findViewById(R.id.rl_detail);
		tv_customer=(TextView)findViewById(R.id.enterprise_tel);
		rl_detail.setOnClickListener(this);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		btn_invest.setOnClickListener(this);
	}
	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("exgold", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					
					JSONObject json1=jsonObject.optJSONObject("data");
					rate_of_return.setText(json1.optString("fixedRate"));
					tv_amount.setText(df.format(Double.parseDouble(json1.optString("experienceAmount"))));
					way=json1.optString("expReceivableWay");
					rate=json1.optString("fixedRate");
					time=json1.optString("experienceDays");
				}else{
					Toast.makeText(ExperienceGoldProduct.this, jsonObject.optString("comment"),2000).show();
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(ExperienceGoldProduct.this, Constant.URL+"/api/v5/experience/experienceDetail/"+id, "");
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(flag==1){
			Intent intent=new Intent(ExperienceGoldProduct.this,MainActivity.class);
			startActivity(intent);
		}else{
			
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch(v.getId()){
		case R.id.btn_invest:
			invest();
			break;
		case R.id.rl_back:
			if(flag==1){
				intent=new Intent(ExperienceGoldProduct.this,MainActivity.class);
				startActivity(intent);
			}else{
				
				finish();
			}
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		case R.id.rl_detail:
			intent=new Intent(ExperienceGoldProduct.this,ExperienceProductDeaily.class);
			intent.putExtra("way", way);
			intent.putExtra("time", time);
			intent.putExtra("rate", rate);
			intent.putExtra("id", id);
			
			startActivity(intent);
			break;
		}
		
	}

}
